package my;

import my.printer.BinaryTrees;
import my.tree.BST;
import my.tree.BinaryTree;

/**
 * <p>
 *
 * </p>
 *
 * @author AJun
 * @since 2020/9/15
 */

public class Main16 {

    public static void main(String[] args) {
        // 创建BST
        Integer[] data = new Integer[]{7, 4, 9, 2, 5, 8, 11};
        BST<Integer> bst = new BST<>();
        for (Integer n : data)
            bst.add(n);

        // 树状打印
        BinaryTrees.println(bst);
        // 遍历器
        StringBuilder sb = new StringBuilder();
        BinaryTree.Visitor<Integer> visitor = element -> {
            sb.append(element).append(" ");
            return false;
        };
        // 遍历
        sb.delete(0, sb.length());
        bst.preorder(visitor);
        Asserts.test(sb.toString().equals("7 4 2 5 9 8 11 "));

        sb.delete(0, sb.length());
        bst.inorder(visitor);
        Asserts.test(sb.toString().equals("2 4 5 7 8 9 11 "));

        sb.delete(0, sb.length());
        bst.postorder(visitor);
        Asserts.test(sb.toString().equals("2 5 4 8 11 9 7 "));
    }

}

